package com.vincent.sciinfo.service.impl;


import com.vincent.sciinfo.mapper.PatentMapper;
import com.vincent.sciinfo.pojo.Patent;
import com.vincent.sciinfo.pojo.PatentExample;
import com.vincent.sciinfo.service.PatentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class PatentServiceImpl implements PatentService {

    @Autowired
    private PatentMapper patentMapper;

    /**
     * 根据教师Id 查询制定状态下的 专利
     *
     * @param ope       要查询专利的状态
     * @param teacherId 教师Id
     * @return
     */
    @Override
    public List<Patent> selectPatent(int ope, String teacherId) {
        PatentExample patentExample = new PatentExample();
        PatentExample.Criteria circle = patentExample.createCriteria();
        circle.andTeacheridEqualTo(teacherId);
        circle.andCheckflagEqualTo(ope);
        return patentMapper.selectByExample(patentExample);
    }

    /**
     * 根据教师Id 查询制定状态下的 专利
     *
     * @param patent 欲插入的专利
     * @return
     */
    @Override
    @Transactional
    public int insertPatent(Patent patent) {
        int codeInsert = patentMapper.insert(patent);
        int codeScore = patentMapper.updatePatentScoreByType(patent.getResultuuid(), patent.getPatenttype());
        return codeInsert + codeScore;
    }

    /**
     * 查找出所有，通过审核或者被下架的专利
     */
    @Override
    public List<Patent> selectCheckAndRemovePatent() {
        PatentExample patentExample = new PatentExample();
        PatentExample.Criteria circle = patentExample.createCriteria();
        List<Integer> vals = new LinkedList<>();
        vals.add(0);
        vals.add(1);
        vals.add(3);
        circle.andCheckflagIn(vals);
        return patentMapper.selectByExample(patentExample);
    }

    @Override
    public void updatePatentStatus(String patentId, int operaCode) {
        Patent patent = patentMapper.selectByPrimaryKey(patentId);
        patent.setCheckflag(operaCode);
        patentMapper.updateByPrimaryKey(patent);
    }

    @Override
    public String[] selectPatentNameById(String patentId) {
        String[] strings = new String[2];
        Patent patent = patentMapper.selectByPrimaryKey(patentId);
        strings[0] = patent.getTeacherid();
        strings[1] = patent.getPatentname();
        return strings;
    }
}
